package com.example.teaculture.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebBackForwardList;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.example.teaculture.DB.FavorDao;
import com.example.teaculture.DB.HistoryDao;
import com.example.teaculture.R;
import com.example.teaculture.tools.Favor;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

public class ZhidaoFragment extends androidx.fragment.app.Fragment {
    private WebView webview;
    private FloatingActionButton mfab;

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.zhidao_fragment, container, false);
        webview = (WebView) view.findViewById(R.id.web_zhidao);
        mfab = (FloatingActionButton)view.findViewById(R.id.fab);
        WebSettings settings = webview.getSettings();
        settings.setJavaScriptEnabled(true);

        //支持缩放
        settings.setUseWideViewPort(true);//设定支持viewport
        settings.setLoadWithOverviewMode(true);
        settings.setBuiltInZoomControls(true);
        settings.setSupportZoom(true);//设定支持缩放

        // 设置与Js交互的权限
        settings.setJavaScriptEnabled(true);
        webview.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                Log.i("title", "onReceivedTitle: "+ title);
                Favor history=new Favor();
                HistoryDao function=new HistoryDao(ZhidaoFragment.super.getContext());
                WebBackForwardList webBackForwardList = webview.copyBackForwardList();
                int posion = webBackForwardList.getCurrentIndex();
                history.setTitle(title);
                history.setUrl(webBackForwardList.getItemAtIndex(posion).getUrl());
                function.insertDetsilNews(history);
            }
        });

        webview.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return super.shouldOverrideUrlLoading(view, request);
            }
        });

        //打开的网址
        webview.loadUrl("http://www.zgchawang.com/knowledge/");
        webview.setOnKeyListener(new View.OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
                    webview.goBack();
                    return true;
                }
                return false;
            }
        });

        mfab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FavorDao favorDao = new FavorDao(ZhidaoFragment.super.getContext());
                Favor favor = new Favor();
                if (mfab.getTag().equals("收藏")) {
                    WebBackForwardList webBackForwardList = webview.copyBackForwardList();
                    int posion = webBackForwardList.getCurrentIndex();
                    String title = webBackForwardList.getItemAtIndex(posion).getTitle();
                    String url = webBackForwardList.getItemAtIndex(posion).getUrl();
                    favor.setTitle(title);
                    favor.setUrl(url);
                    favorDao.insertDetsilNews(favor);
                    Toast.makeText(getActivity(), "收藏成功", Toast.LENGTH_LONG).show();
                    favorDao.findSelected();
                    mfab.setTag("取消收藏");
                }
                else {
                    favorDao.del(favor.getDocid());
                    Toast.makeText(getActivity(), "取消收藏", Toast.LENGTH_LONG).show();
                    mfab.setTag("收藏");
                }
            }
        });
        return view;
    }
}
